package offer.offer01;

/**
 *
 */
public class S16数值的整数次方 {
    /**
     * 总的来说就是对幂指数进行减半操作来降低计算次数到 logn
     */
    public double myPow(double x, int n) {
        if(n == 1) return x;
        if(n == -1) return 1/x;
        if(n%2 == 0) {
            double tmp1 = myPow(x, n/2);
            return tmp1*tmp1;
        }else if(n%2 == 1) {
            double tmp2 = myPow(x, n/2);
            return x*tmp2*tmp2;
        }else {
            double tmp3 = myPow(x, n/2);
            return 1/x*tmp3*tmp3;
        }
    }
}
